# -*- coding: utf-8 -*-
"""
Created on Sat Aug 18 14:10:55 2012

@author: witcher112
"""

import CPPparse
import sys

class ReplaceType:
    def __init__(self,oldName,oldExtra,oldArray,newName,newExtra,newArray):
        self.OldName=oldName
        self.OldExtra=oldExtra
        self.OldArray=oldArray
        self.NewName=newName
        self.NewExtra=newExtra
        self.NewArray=newArray

class ConvertObject:
    def __init__(self,nm,ptr,ref,defTemplate):
        self.pointer=ptr=="True"
        self.reference=ref=="True"
        self.definedTemplates=[]
        self.name=nm
        defTable=defTemplate.split(";")
        for x in defTable:
            self.definedTemplates.append(x.split("="))
        
        
replaceTypes=[]
convertObjects=[]
globalNamespace=CPPparse._Namespace()
mode=sys.argv[1]
scanFile=sys.argv[2]
outFile=sys.argv[3]
cfg1=sys.argv[4]
cfg2=sys.argv[5]
def writeNamespaceMode0(n,f):
    if n["name"]!="":
        f.write("NAMESPACE"+"\n")
        f.write(n["name"]+"\n")
    for t in n["namespaces"]:
        writeNamespaceMode0(t,f)
    for t in n["classes"]:
        f.write("CLASS"+"\n")
        f.write(t["name"]+"\n")
    for t in n["typedefs"]:
        if t["name"]==None:
            continue
        f.write("TYPEDEF"+"\n")
        f.write(t.data["name"]+"\n")
    for t in n["functions"]:
        f.write("FUNCTION"+"\n")
        f.write(t["name"]+"\n")
    if n["name"]!="":
        f.write("ENDNS"+"\n")

def readConfigMode0(fname):
    f=open(fname,"r")
    rstr=f.readline()
    f.close()
    return rstr

def replaceTypeInType(t):
    nt=CPPparse._Type()
    nt.data=t.data
    for r in replaceTypes:
        if t["name"]==r.OldName or r.OldName=="__ANY__":
            if t["extra"]==r.OldExtra or r.OldExtra=="__ANY__":
                if t["array"]==r.OldArray or r.OldArray=="__ANY__":
                    if r.NewName!="__SAME__":
                        nt.data["name"]=r.NewName
                    if r.NewExtra!="__SAME__":
                        nt.data["extra"]=r.NewExtra
                    if r.NewArray!="__SAME__":
                        nt.data["array"]=r.NewArray
    for x in range(0,len(nt["templates"])):
        nt["templates"][x]=replaceTypeInType(nt["templates"][x])
    return nt
    

def readConfigMode1(fname):
    f=open(fname,"r")
    readConvertType=False
    readReplaceType=False
    data=[]
    ldata=0
    for x in f.readlines():
        x=x.replace("\n","")
        if readConvertType==False and readReplaceType==False:
            
            if x=="REPLACE_TYPE":
                readReplaceType=True
                ldata=0
                data=[]
            elif x=="CONVERT_TYPE":
                readConvertType=True
                ldata=0
                data=[]
        elif readConvertType==True:
            if x=="__NONE__":
                x=""
            data.append(x)
            ldata=ldata+1
            if ldata>3:
                pp=ConvertObject(data[0],data[1],data[2],data[3])
                readConvertType=False
                convertObjects.append(pp)
        elif readReplaceType==True:
            if x=="__NONE__":
                x=""
            data.append(x)
            ldata=ldata+1
            if ldata>5:
                pp=ReplaceType(data[0],data[1],data[2],data[3],data[4],data[5])
                readReplaceType=False
                replaceTypes.append(pp)
    f.close()

if mode=="0":
    #searching for declarations
    globalNamespace=CPPparse.parse(readConfigMode0(cfg1))
    f=open(scanFile,"w")
    
    writeNamespaceMode0(globalNamespace,f)
    f.close()

elif mode=="1":
    readConfigMode1(cfg2)
    globalNamespace=CPPparse.parse(readConfigMode0(cfg1),False)
    
    #it=0
    #for t in globalNamespace.getObjects("Typedef"):
    #    print it,t["type"]["templates"][0]
    #    it=it+1
    for x in globalNamespace["namespaces"]:
        print "-----------------------\n"
        if len(x["namespaces"])==0:
            print x
        else:
            print x["namespaces"][0]
    globalNamespace.mergeNamespace(globalNamespace)
    print "\n\n++++++++++++++++++++++++++\n\n"
    for x in globalNamespace["namespaces"]:
        print x["namespaces"][0]